package com.kingdee.eas.custom.hr.affair.fluctuation.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.org.AdminOrgFacadeFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.IPositionMember;
import com.kingdee.eas.basedata.org.JobInfo;
import com.kingdee.eas.basedata.org.PositionMemberCollection;
import com.kingdee.eas.basedata.org.PositionMemberFactory;
import com.kingdee.eas.basedata.org.PositionMemberInfo;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.affair.fluctuation.OuterFluctuationOrgChangeEntryFactory;
import com.kingdee.eas.custom.hr.affair.fluctuation.OuterFluctuationOrgChangeEntryInfo;
import com.kingdee.eas.custom.hr.base.AttendanceCodeInfo;
import com.kingdee.eas.custom.hr.base.DirectAndIndirectInfo;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.hr.base.IHRBillBaseEntry;
import com.kingdee.eas.hr.base.app.HRBillAppUtils;
import com.kingdee.eas.hr.base.app.IHRBillBizBean;
import com.kingdee.eas.hr.canon.cbase.AdminOrgBaseInfo;
import com.kingdee.eas.hr.canon.cbase.GradeInfo;
import com.kingdee.eas.hr.emp.IPersonWorkExpCur;
import com.kingdee.eas.hr.emp.PersonWorkExpCurFactory;
import com.kingdee.eas.hr.emp.PersonWorkExpCurInfo;
import com.kingdee.eas.util.app.DbUtil;

public class OuterFluctuationOrgChangeControllerBean extends
		AbstractOuterFluctuationOrgChangeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.affair.fluctuation.app.OuterFluctuationOrgChangeControllerBean");

	@Override
	protected String getBillField() throws BOSException, EASBizException {
		return "parent";
	}

	@Override
	protected IHRBillBizBean getBizBean() throws BOSException, EASBizException {
		return new OuterFluctuationOrgChangeBizBean();
	}

	@Override
	protected String getEntryEffectDateField() throws BOSException,
			EASBizException {
		return "effectDate";
	}

	@Override
	protected String getEntryField() throws BOSException, EASBizException {
		return "entrys";
	}

	@Override
	protected IHRBillBaseEntry getEntryInterface(Context ctx)
			throws BOSException, EASBizException {
		return OuterFluctuationOrgChangeEntryFactory.getLocalInstance(ctx);
	}

	@Override
	protected void _entryEffect(Context ctx, HRBillBaseEntryInfo entry)
			throws BOSException, EASBizException {
		OuterFluctuationOrgChangeEntryInfo entryInfo = (OuterFluctuationOrgChangeEntryInfo) entry;
		this.saveWorkExp(ctx, entryInfo);

		String personID = entryInfo.getPerson().getId().toString();
		String positionID = entryInfo.getTargetPosition().getId().toString();

		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(new FilterItemInfo("person.id", personID));
		filter.getFilterItems().add(
				new FilterItemInfo("position.id", positionID));
		filter.setMaskString("#0 and #1");
		EntityViewInfo evi = new EntityViewInfo();
		evi.setFilter(filter);

		IPositionMember ipm = PositionMemberFactory.getLocalInstance(ctx);
		// if (!entryInfo.isIsPluralism()) {
		ipm.delete("where person.id = '" + personID + "' AND isPrimary = 1");
		// }
		PositionMemberCollection pmColl = ipm.getPositionMemberCollection(evi);
		PositionMemberInfo pmInfo = null;
		if (pmColl.size() == 0) {
			pmInfo = getPMInfo(pmInfo, entryInfo);
			ipm.addnew(pmInfo);
		} else {
			pmInfo = pmColl.get(0);
			pmInfo = getPMInfo(pmInfo, entryInfo);
			ipm.update(new ObjectUuidPK(pmInfo.getId()), pmInfo);
		}
		updatePersonInfo(ctx, entryInfo);

		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("person.id", entryInfo.getPerson().getId()
						.toString()));
		fi.getFilterItems().add(
				new FilterItemInfo("endDate", null, CompareType.IS));

		String update = "/*dailect*/UPDATE CT_KQ_SpecialOperationMgrEntry SET CFEndDate=SYSDATE WHERE CFPersonID=? AND CFEndDate IS NULL";
		DbUtil.execute(ctx, update, new Object[] { entryInfo.getPerson()
				.getId().toString() });
	}

	private PositionMemberInfo getPMInfo(PositionMemberInfo pmInfo,
			OuterFluctuationOrgChangeEntryInfo entry) {
		if (pmInfo == null) {
			pmInfo = new PositionMemberInfo();
		}
		pmInfo.setPosition(entry.getTargetPosition());
		pmInfo.setPerson(entry.getPerson());
		pmInfo.setIsPrimary(true);
		pmInfo.setHrOrgUnit(entry.getParent().getHrOrgUnit());
		pmInfo.setBeginDate(entry.getEffectDate());
		pmInfo.setCostCenter(entry.getTargetAdminBase());
		return pmInfo;
	}

	private void updatePersonInfo(Context ctx, OuterFluctuationOrgChangeEntryInfo entry)
			throws EASBizException, BOSException {
		PersonInfo person = entry.getPerson();

		person = PersonFactory.getLocalInstance(ctx).getPersonInfo(
				new ObjectUuidPK(person.getId()));

		AdminOrgBaseInfo adminBaseInfo = entry.getTargetAdminBase();

		JobInfo jobInfo = entry.getTargetJob();
		AttendanceCodeInfo attendCode = entry.getTargetAttendCode();
		DirectAndIndirectInfo directInfo = entry.getTargetDirectDiff();
		GradeInfo gradeInfo = entry.getTargetGrade();
		AdminOrgUnitInfo adminOrgUnit = entry.getTargetAdminOrgUnit();

		person.setAdminOrgBase(adminBaseInfo);
		person.setDuty(jobInfo);
		person.setAttendanceCode(attendCode);
		person.setDirectAndIndirectDiff(directInfo);
		person.setGrade(gradeInfo);
		person.setDept(adminOrgUnit);
		person.setIsSpecialCode(false);
		PersonFactory.getLocalInstance(ctx).update(
				new ObjectUuidPK(person.getId()), person);
	}

	private void saveWorkExp(Context ctx, OuterFluctuationOrgChangeEntryInfo entry)
			throws BOSException, EASBizException {
		if (entry.isIsPluralism()) {
			return;
		}
		IPersonWorkExpCur ipwxc = PersonWorkExpCurFactory.getLocalInstance(ctx);
		PersonInfo pInfo = entry.getPerson();

		PersonWorkExpCurInfo pwInfo = null;

		pwInfo = new PersonWorkExpCurInfo();
		pwInfo.setPerson(pInfo);

		PositionMemberInfo pmInfo = HRBillAppUtils
				.getPMInfoByPersonAndPosition(ctx, pInfo.getId(), entry
						.getNowPosition().getId());

		if (pmInfo == null) {
			return;
		}
		pwInfo.setPosition(entry.getTargetPosition());

		pwInfo.setBeginDate(pmInfo.getBeginDate());
		pwInfo.setEndDate(entry.getEffectDate());
		pwInfo.setJob(entry.getTargetJob());
		AdminOrgUnitInfo[] admins = AdminOrgFacadeFactory.getLocalInstance(ctx)
				.getComAndDeptByPositionId(
						entry.getNowPosition().getId().toString());

		pwInfo.setDept(entry.getTargetAdminOrgUnit());

		// pwInfo.set

		pwInfo.setDescription(entry.getRemark());
		pwInfo.setJobGradeDate(entry.getEffectDate());

		pwInfo.setIsPrimary(pmInfo.isIsPrimary());
		pwInfo.setPersonRZType(pmInfo.getPersonRZType());

		pwInfo.setPastPositon(pmInfo.getPosition().getName());
		if ((admins[0] != null) && (admins[0].getDisplayName() != null)) {
			pwInfo.setPastDept(admins[0].getDisplayName());
		}
		if ((pmInfo.getPosition().getJob() != null)
				&& (pmInfo.getPosition().getJob().getName() != null)) {
			pwInfo.setPastJob(pmInfo.getPosition().getJob().getName());
		}
		if ((pmInfo.getPersonRZType() != null)
				&& (pmInfo.getPersonRZType().getName() != null)) {
			pwInfo.setPastRZType(pmInfo.getPersonRZType().getName());
		}

		pwInfo.setCheckLen(pmInfo.getCheckLen());
		pwInfo.setCheckOverTime(pmInfo.getCheckOverTime());
		pwInfo.setIsCheck(pmInfo.isIsCheck());

		pwInfo.setOldCostCenter(entry.getNowAdminBase());
		pwInfo.setCostCenter(entry.getTargetAdminBase());
		ipwxc.addnew(pwInfo);
	}

	@Override
	protected String getEffectFunction() throws BOSException, EASBizException {
		return "com.kingdee.eas.custom.hr.affair.fluctuation.app.OuterFlucationOrgChangeEffectEntryFunction";
	}

}